package xm.manager
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import xm.core.QueuePriority;
	import xm.core.XMSprite;
	import xm.events.XMStateEvent;
	import xm.interfaces.IXMLayout;
	import xm.namespaces.xprimeMobile;
	import xm.view.ViewNavigatorApplication;
	
	
	//-----------------------------------------------------------------------------------
	// namespace
	//-----------------------------------------------------------------------------------
	use namespace xprimeMobile;
	
	
	/**
	 * <br> 클래스 설명 : layout 총괄 메니저.</br>
	 * @author 김정환
	 */	
	public class LayoutMananger extends EventDispatcher implements IXMLayout
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		private var _invalidatePropertiesQueue : QueuePriority = new QueuePriority();
		private var _invalidateChildrenQueue : QueuePriority = new QueuePriority();
		private var _invalidateDisplayListQueue : QueuePriority = new QueuePriority();
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function LayoutMananger( application : XMSprite )
		{
//			application.addEventListener(Event.ENTER_FRAME, onEnterFrame );
		}
			
		
		/**
		 * @param obj : XMSprite
		 */		
		public function invalidatePropertiesQueue( obj:XMSprite ):void
		{
			if( obj.invalidatePropertiesFlag == true )
			{
				_invalidatePropertiesQueue.addQueue( obj, validateProperties );
			}
			
			_invalidatePropertiesQueue.executeQueue();
		}
		
		/**
		 * @param obj : XMSprite
		 */		
		public function invalidateChildrenQueue( obj:XMSprite ):void
		{
			if( obj.invalidateChildrenFlag == true )
			{
				_invalidateChildrenQueue.addQueue( obj, validateChildren );
			}
			
			_invalidateChildrenQueue.executeQueue();
		}
		
		/**
		 * @param obj : XMSprite
		 */		
		public function invalidateDisplayListQueue( obj:XMSprite ):void
		{
			if( obj.invalidateDisplayListFlag == true )
			{
				_invalidateDisplayListQueue.addQueue( obj, validateUpdateDisplayList );
			}
			
			_invalidateDisplayListQueue.executeQueue();
		}
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		private function validateProperties( obj:XMSprite ):void
		{
			obj.commitProperties();
			obj.dispatchEvent( new XMStateEvent( XMStateEvent.STATE_COMMIT_PROPERTIES_COMPLETE, false, false ) );
			obj.invalidatePropertiesFlag = false;
		}
		
		private function validateChildren( obj:XMSprite ):void
		{
			obj.createChildren();
			obj.creationComplete();
			obj.dispatchEvent( new XMStateEvent( XMStateEvent.STATE_CREATION_COMPLETE, false, false ) );
			obj.invalidateChildrenFlag = false;
		}
		
		private function validateUpdateDisplayList( obj:XMSprite ):void
		{
			obj.updateDisplayList();
			obj.dispatchEvent( new XMStateEvent( XMStateEvent.STATE_UPDATE_DISPLAY_LIST_COMPLETE, false, false ) );
			obj.invalidateDisplayListFlag = false;
		}
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		/**
		 * @param e
		 */		
		protected function onEnterFrame(e:Event):void
		{
			_invalidatePropertiesQueue.executeQueue();
			_invalidateChildrenQueue.executeQueue();
			_invalidateDisplayListQueue.executeQueue();
		}	
		
		
		
		
		
		
		
	}
}